package com.ry.demo.concurrent.demo.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: lihui
 * @Date: 2021-12-30 09:11
 * @Description: 用Condition实现await和notify的效果
 */
public class ConditionDemo {

    public static void main(String[] args) throws Exception{
        ReentrantLock reentrantLock = new ReentrantLock();
        Condition condition = reentrantLock.newCondition();


        new Thread(()->{
            reentrantLock.lock();
            System.out.println("线程1进入阻塞");
            try {
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程1从阻塞中苏醒");
            reentrantLock.unlock();
        }).start();

        Thread.sleep(1000);


        new Thread(()->{
            reentrantLock.lock();
            System.out.println("线程2唤醒线程1");
            condition.signal();

            reentrantLock.unlock();
        }).start();
    }

}
